<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/base_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Logbook.php');

class logbook_DA extends base_DA{

    const FL_ID = 'id';
    const FL_TYPE = 'type';
    const FL_TEXT = 'text';
    const FL_DATE = 'date';
    const FL_FK_GUILD_ID = 'fk_guild_id';


    /**
     * (PHP 5)<br/>
     * Create logbook entry
     * @param int $type
     * @param string $text
     * @param int $id_guild
     */
    public function create_logbook_entry($type, $text, $id_guild){
        $sql_cmd = 'INSERT INTO '.$this->get_table_prefix().'_logbook ('.logbook_DA::FL_FK_GUILD_ID.', '.logbook_DA::FL_TYPE.', '.logbook_DA::FL_TEXT.',
                    '.logbook_DA::FL_DATE.')
            VALUES ('.$id_guild.','.$type.','.$this->format_string_to_sql_param($text).','
                    .$this->format_date_to_sql_param(date('Y-m-d H:i:s',time())).')';

//        echo '<br>logbook_DA->create_logbook_entry : '.$sql_cmd;

        $this->execute_sql_command($sql_cmd, true);
    }

    /**
     * (PHP 5)<br/>
     * Get all logbook entry
     * @param int $guild_id
     * @return array of Log
     */
    public function get_guild_logbook_entry($guild_id){
        $sql_cmd = 'SELECT * FROM '.$this->get_table_prefix().'_logbook WHERE '.logbook_DA::FL_FK_GUILD_ID.' = '.$guild_id.' ORDER BY '.logbook_DA::FL_DATE.' DESC';

//        echo '<br>logbook_DA->get_all_logbook_entry : '.$sql_cmd;

        $result = $this->execute_sql_command($sql_cmd, false);


        if (isset($result[0])){
            foreach ($result as $l){
                $log_array[] = new Log($l[logbook_DA::FL_ID], $l[logbook_DA::FL_TYPE], $l[logbook_DA::FL_TEXT],
                        $l[logbook_DA::FL_DATE]);
            }
        }

         return (isset($log_array) ? $log_array : null);
    }
}
?>
